package com.itheima.controller;

import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result2;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/validatecode")
@Slf4j
public class ValidateCodeController {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 发送验证码
     * @param telephone
     * @return
     */
    @PostMapping("/send4Order")
    public Result2 send4Order(String telephone) {
        log.info("发送验证码：{}", telephone);
        //生成一个随机4位数字字符串
        String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
        log.info("生成的验证码：{}", validateCode);
        //存入redis中并设置5分钟后自动删除
        redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_ORDER+telephone, validateCode, 5, java.util.concurrent.TimeUnit.MINUTES);


        return new Result2(true, "验证码发送成功", validateCode);
    }
    @GetMapping("/send4Login")
    public Result2 send4Login(String telephone) {
        log.info("发送验证码：{}", telephone);
        //生成一个随机6位数字字符串
        String validateCode = ValidateCodeUtils.generateValidateCode(6).toString();
        log.info("生成的验证码：{}", validateCode);
        //存入redis中并设置5分钟后自动删除
        redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_LOGIN+telephone, validateCode, 5, java.util.concurrent.TimeUnit.MINUTES);
        return new Result2(true, "验证码发送成功", validateCode);
    }
}
